// pages/activities/activities.js
wx.cloud.init({
  appid:'wx65de05153456b8d1'
});
const db = wx.cloud.database()
const _ = db.command;
Page({

      /**
       * 页面的初始数据
       */
      data: {
            img: '../../image/',
            city: '未选择',
            resdata: '',
            province: []
      },
      // 选择城市
      bindRegionChange(e) {
            //this.getinfo()
            var reg = /.+?(省|市|自治区|自治州|县|区)/g;
            console.log(e.detail.value[0]+e.detail.value[1])
            this.setData({
                  city: e.detail.value,
                  // 'province':address.match(reg)
            })
            var resdatapro = this.data.resdata

            for (let i = resdatapro.data.length - 1; i >= 0; i--) {
                  //console.log(resdatapro[i].address)
                  var address = this.extractCity(resdatapro.data[i].address).city
                  console.log(address) 
                  if (e.detail.value[0] + e.detail.value[1] !== address) {
                        resdatapro.data.splice(i, 1)
                  }
            }
            this.setData({
                  'resdata':resdatapro
            })
      },

      //地址提取函数
      extractCity(address) {
            var cityPattern = /(.*?(?:市|自治州|地区))(?!.*?(?:省|自治区|特别行政区))/;
            var cityMatch = address.match(cityPattern);
            var city = cityMatch ? cityMatch[1] : "";
            //console.log(city)
            return {
                  city: city
            };
      },



      getinfo() {
            db.collection('activity').where({
                        flag: 1
                  }).get()
                  .then(e => {
                        //console.log(e)
                        this.setData({
                              'resdata': e
                        })
                  })
      },
      navigateToDetail(event) {
            var index = event.currentTarget.dataset.index; // 获取点击项的索引
            var psdata = this.data.resdata.data[index]; // 获取点击项的数据
            //console.log(this.data.resdata.data[index])
            wx.navigateTo({
                  url: `../../subpackages/request/detail/detail?data=${JSON.stringify(psdata)}`, // 传递数据到详情页
            });
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad(options) {
            this.getinfo()
      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady() {
            this.getinfo()

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow() {

      },

      /**
       * 生命周期函数--监听页面隐藏
       */
      onHide() {

      },

      /**
       * 生命周期函数--监听页面卸载
       */
      onUnload() {

      },

      /**
       * 页面相关事件处理函数--监听用户下拉动作
       */
      onPullDownRefresh: function () {
            this.onRefresh();
            this.getinfo();
      },
      onRefresh: function () {
            //导航条加载动画
            wx.showNavigationBarLoading();
            setTimeout(function () {
                  wx.hideNavigationBarLoading();
                  //停止下拉刷新
                  wx.stopPullDownRefresh();
            }, 2000);
      },

      /**
       * 页面上拉触底事件的处理函数
       */
      onReachBottom() {

      },

      /**
       * 用户点击右上角分享
       */
      onShareAppMessage() {

      }
})